<?php
namespace Database;

class UserIncomeLog extends Base
{

    public function __construct()
    {
        $this->setModel(MODEL_USERINCOMELOG);
    }

    public function getList($userId, $page, $limit)
    {
        $sql = $this->getSql();
        $sql->field('*');
        $sql->table($this->getTable());
        $sql->where('dry_user', '=', $userId);
        $sql->setOrder('id', 'desc');
        $sql->setPage($page);
        $sql->setPageSize($limit);
        $sql->setLimit();
        $list = $this->fetchAll($sql->get());
        $count = $this->fetchColumn($sql->getTotalSql());
        return [
            'list' => $list,
            'count' => $count
        ];
    }

    public function getSum($userId, $typeIn = '', $month = '', $startDate = '', $endDate = '')
    {
        $sql = $this->getSql();
        $sql->field('ifnull(sum(dry_amount),0) as total');
        $sql->table($this->getTable());
        $sql->where('dry_user', '=', $userId);
        if($typeIn != ''){
            $sql->andWhere('dry_type', 'in', $typeIn);
        }
        if($month != ''){
            $sql->andWhere('dry_month', '=', $month);
        }
        if($startDate != ''){
            $sql->andWhere('dry_add_date', '>=', $startDate);
        }
        if($endDate != ''){
            $sql->andWhere('dry_add_date', '<=', $endDate);
        }
        return $this->fetchColumn($sql->get());
    }

}